package com.group.Service.Impl;

import com.group.DAO.UserDAO;
import com.group.Enity.User;
import com.group.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDAO userDAO;

    @Override
    public User getUser(User u) {


        if (u != null && u.getPassword() != null)
        {
            String pwdMd5 = DigestUtils.md5DigestAsHex(u.getPassword().getBytes());
            u.setPassword(pwdMd5);
        }else
        {
            return null;
        }
        return userDAO.getUser(u);
    }

    @Override
    public void addUser(String name,String pwd){

        User u = new User();
        u.setUsername(name);
        String pwdMd5 = DigestUtils.md5DigestAsHex(pwd.getBytes());
        u.setPassword(pwdMd5);
        userDAO.addUser(u);
    }

    @Override
    public void changePwd(int id, String pwd) {
        //System.out.println("id:" + id + "pwd:" + pwd);
        String pwdMd5 = DigestUtils.md5DigestAsHex(pwd.getBytes());
        userDAO.setPwd(id,pwdMd5);

    }

    @Override
    public boolean checkPwd(int id, String pwd) {
        String oldpwd = userDAO.getPwd(id);
        String pwdMd5 = DigestUtils.md5DigestAsHex(pwd.getBytes());
        if (oldpwd.equals(pwdMd5))
            return true;
        return false;
    }
}
